/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Backup.java
 *
 * Created on Nov 21, 2011, 7:09:26 AM
 */
package form;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import ultilities.Ultilities;
import javax.swing.JFrame;

/**
 *
 * @author bi
 */
public class Backup extends JFrame {

    JFrame parent;

    /** Creates new form Backup */
    public Backup(JFrame parent) {
        this.parent = parent;
        initComponents();
        this.setLocation(300, 100);

        checkBackup();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnBackupSave = new javax.swing.JButton();
        btnBackupLoad = new javax.swing.JButton();
        cbxBackupList = new javax.swing.JComboBox();
        btnBackupClose = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Warhammer 40.000: Text Game - Backup");
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Warhammer 40.000: Text Game");
        jPanel1.add(jLabel1);

        getContentPane().add(jPanel1);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        btnBackupSave.setText("Save");
        btnBackupSave.setMaximumSize(new java.awt.Dimension(100, 30));
        btnBackupSave.setMinimumSize(new java.awt.Dimension(100, 30));
        btnBackupSave.setPreferredSize(new java.awt.Dimension(100, 30));
        btnBackupSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackupSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(btnBackupSave, gridBagConstraints);

        btnBackupLoad.setText("Load");
        btnBackupLoad.setMaximumSize(new java.awt.Dimension(100, 30));
        btnBackupLoad.setMinimumSize(new java.awt.Dimension(100, 30));
        btnBackupLoad.setPreferredSize(new java.awt.Dimension(100, 30));
        btnBackupLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackupLoadActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(btnBackupLoad, gridBagConstraints);

        cbxBackupList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(cbxBackupList, gridBagConstraints);

        btnBackupClose.setText("Close");
        btnBackupClose.setMaximumSize(new java.awt.Dimension(100, 30));
        btnBackupClose.setMinimumSize(new java.awt.Dimension(100, 30));
        btnBackupClose.setPreferredSize(new java.awt.Dimension(100, 30));
        btnBackupClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackupCloseActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(btnBackupClose, gridBagConstraints);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Backup Date:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(jLabel2, gridBagConstraints);

        getContentPane().add(jPanel2);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBackupSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupSaveActionPerformed
        Date date = new Date(System.currentTimeMillis());
        String filename = "backup/" + (date.getYear()+1900) + "-" + (date.getMonth()+1) + "-" + date.getDate()
                + "." + date.getHours() + "-" + date.getMinutes() + "-" + date.getSeconds() + ".zip";
        Ultilities.saveZipFile("xml", filename);
        checkBackup();
    }//GEN-LAST:event_btnBackupSaveActionPerformed

    private void btnBackupLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupLoadActionPerformed
        Ultilities.loadZipFile("backup/"+cbxBackupList.getSelectedItem().toString()+".zip", "");
    }//GEN-LAST:event_btnBackupLoadActionPerformed

    private void btnBackupCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupCloseActionPerformed
        this.dispose();
        parent.setVisible(true);
    }//GEN-LAST:event_btnBackupCloseActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBackupClose;
    private javax.swing.JButton btnBackupLoad;
    private javax.swing.JButton btnBackupSave;
    private javax.swing.JComboBox cbxBackupList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

    private void checkBackup() {
        Vector<String> backup = new Vector<String>();
        File bkFolder = new File("backup");
        String[] list = bkFolder.list();
        for (String s : list) {
            backup.add(s.substring(0, s.length() - 4));
        }

        cbxBackupList.setModel(new DefaultComboBoxModel(backup));
    }
}
